package uk.ac.strath.cis.religionsearch.controller;

import java.util.ArrayList;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.lucene.document.Document;

import uk.ac.strath.cis.religionsearch.view.SearchWindow;
import uk.ac.strath.cis.religionsearch.model.Model;

public class Controller {
	
	private Model model;
	private SearchWindow gui;
	
	public Controller(Model _model){
		model = _model;
		gui = new SearchWindow(this);

		SwingUtilities.invokeLater(new Runnable() {	
			public void run() {
				try{
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				}
				catch (Exception e) {
					e.printStackTrace();
				}
				gui.init();
				model.addObserver(gui);
			}
		});	
	}
	
	public void searchPressed(String _query){
		try{
			model.search(_query, true, true, true, false);
		} catch(Exception e) {
			System.err.println("Search error " + e);
		}
	}
	
	public void applyBookFilter(int i){
		try {
			model.filterResultsBook(i);
		} catch (Exception e) {
		}
	}
	
	public void applyTstmtFilter(int i){
		try {
			model.filterResultsRText(i);
		} catch (Exception e) {
		}
	}
	
	public void updateTags(String tags){
		model.updateTags(tags);
	}
	
	private static String RemoveSpecialCharacters(String str) {
		char c = ' ';
		   StringBuilder sb = new StringBuilder();
		   for(int i=0;i<str.length();i++) {
			   c=str.charAt(i);
		      if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z')  || (c >= 'a' && c <= 'z') || c == ' ') {
		         sb.append(c);
		      }
		   }
		   return sb.toString();
		}

	private static String arrayToString(String[] a) {
	    StringBuffer result = new StringBuffer();
	    String separator = " ";
	    if (a.length > 0) {
	        result.append(a[0]);
	        for (int i=1; i<a.length; i++) {
	            result.append(separator);
	            result.append(a[i]);
	        }
	    }
	    return result.toString();
	}
	
	public String[] getPreview(int index){
		String[] info = new String[5];
		try {
		if(index>=0){
		StringBuilder sb = new StringBuilder();
		Document doc = model.getDocumentAtIndex(index);
		
		String query = model.getQuery();
		query = RemoveSpecialCharacters(query);
		String[] queryTokens = query.split(" ");
		
		String verse = (doc.get("verse"));
		verse = RemoveSpecialCharacters(verse);
		String[] verseTokens = verse.split(" ");
		
		ArrayList<Integer> highlightedWords = new ArrayList<Integer>(); 
		
		for(int i=0;i<queryTokens.length;i++){
			for(int j=0;j<verseTokens.length;j++){
				if(queryTokens[i].toLowerCase().equals(verseTokens[j].toLowerCase()) && !queryTokens[i].equals("AND") && !queryTokens[i].equals("OR") && !queryTokens[i].equals("NOT")){
					
					if(!highlightedWords.contains(new Integer(j)))
						highlightedWords.add(new Integer(j));
			}
		}
		}
			verseTokens = (doc.get("verse")).split(" ");
			for(int k=0;k<highlightedWords.size();k++){
			verseTokens[highlightedWords.get(k)] = "<span style=\"background: blue; color: white;\">" + verseTokens[highlightedWords.get(k)] + "</span>";
				//verseTokens[highlightedWords.get(k)] = "<b>" + verseTokens[highlightedWords.get(k)] + "</b>";
		}
		

		
		
			info[0] =  arrayToString(verseTokens);
			info[1] = (doc.get("title"));
			info[2] = (doc.get("bktlong"));
			info[3] = (doc.get("verseAndChapterNumbers"));
			info[4] = (doc.get("tags"));
			//System.out.println("qwerty");
			return info;
		} 
		} catch (Exception e) {
		}
		info[0] = "";
		info[1] = "";
		info[2] = "";
		info[3] = "";
		info[4] = "";
		return info;
	}
	

	
	public String getIndexLoc(){
		return model.getIndexSrcFilesLoc();
	}
	
	public void setIndexLoc(String loc){
		model.setIndexSrcFilesLoc(loc);
	}

	public void setIndexDest(String indexDest) {
		model.setIndexDest(indexDest);
		
	}

	public boolean startIndexing() {
		return(model.startIndexing());		
	}
	
}
